EventReplayEmitter
An extension to Node's EventEmitter that supports event replay. In other words,
if a listener is added using either addListenerWithReplay
or onWithReplay
,
then it will instantly trigger the listener with previous event data (if no data
was previously emitted, then the listener will not get triggered on
registration).
Usage Examples
In order to get event replay on listener registration, simply utilize the
addListenerWithReplay
method like so:
import { ReplayEventEmitter } from "replay-event-emitter";
const eventEmitter = new ReplayEventEmitter();
eventEmitter.emit("exampleEvent", "someData");
eventEmitter.addListenerWithReplay("exampleEvent", (data) => {
console.log(data);
});
You could also use the shorthand method onWithRepeat
, like so:
eventEmitter.onWithReplay("exampleEvent", (data) => {
console.log(data);
});
All the original functionality of the EventEmitter is still intact:
import { ReplayEventEmitter } from "replay-event-emitter";
const eventEmitter = new ReplayEventEmitter();
eventEmitter.emit("exampleEvent", "someData");
eventEmitter.addListener("exampleEvent", (data) => {
console.log(data);
});